package com.peng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.peng.pojo.Admin;
import com.peng.service.AdminService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize")Integer pageSize,
            String adminName
    ){
        IPage<Admin> iPage= adminService.getAllAdmin(pageNo,pageSize,adminName);
        return Result.ok(iPage);
    }

    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin){
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> list){
        for (Integer integer : list) {
            Admin byId = adminService.getById(integer);
            String portraitPath = byId.getPortraitPath();
            String path ="F:/学习笔记/myproject/zhxy_db/src/main/resources/public/";
            String str=path+portraitPath;
            new File(str).delete();
        }
        adminService.removeByIds(list);
        return Result.ok();

    }
}
